<template>
  <a-modal
    :title="title"
    :width="800"
    :ok=false
    :visible="visible"
    :confirmLoading="confirmLoading"
    :okButtonProps="{ props: {disabled: disableSubmit} }"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="收货人">
          <span>{{model.contactPerson}}</span>
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="收货电话">
          <span>{{model.contactPhone}}</span>
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="收货地址">
          <span>{{model.contactAddress}}</span>
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="logisticsName"
          label="物流公司">
          <a-input placeholder="请输入物流公司" v-model="model.logisticsName"/>
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="logisticsNo"
          label="物流单号">
          <a-input placeholder="请输入物流单号" v-model="model.logisticsNo"/>
        </a-form-model-item>
        <a-form-model-item 
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="发货时间"
          prop="logisticsTime">
          <a-date-picker
            @change="onChange"
            format="YYYY-MM-DD HH:mm:ss"
            :disabled-date="disabledDate"
            :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
            v-model="model.logisticsTime"
            :style="{ width: '100%' }"
            placeholder="请选择发货时间"
          />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import { getAction, postAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from 'moment'

  export default {
    name: "LogisticsModal",
    data () {
      return {
        title:"物流发货",
        visible: false,
        disableSubmit:false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
          logisticsName:[{ required: true, message: '请输入物流公司!' }],
          logisticsNo: [{ required: true, message: '请输入物流单号!' }],
          logisticsTime:[{required: true, message: '请选择发货时间!'}]
        },
      }
    },
    created () {
    },
    methods: {
      moment,
      disabledDate(current) {
        // Can not select days before today and today
        return current && current > moment().endOf('day');
      },
      onChange(date, dateString) {
        this.model.logisticsTime = dateString
      },
      loadData(orderId){
        var that = this;
        getAction('/shop/order/logistics', {orderId}).then((res)=>{
          if(res.success){
            this.model = res.result
          } else {
            this.$message.error(res.message)
            setTimeout(_ => {
              this.close()
            }, 1500)
          }
        })
      },
      edit (record) {
        console.log(record)
          this.visible = true;
          this.model = Object.assign({}, record)
          this.loadData(record.id);
      },
      close () {
        this.$emit('close');
        this.disableSubmit = false;
        this.visible = false;
        this.$refs.form.resetFields();
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            postAction('/shop/order/logistics/save', this.model).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })

          }else{
            return false;
          }
        })
      },
      handleCancel () {
        this.close()
      }
    }
  }
</script>

<style scoped>

</style>